﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MQTTnet.Server;
using OperationDataBase;
using System.Text;
using System.Threading.Tasks;
namespace Mqtt.Callback.Handlers
{
    /// <summary>
    /// 发布拦截
    /// </summary>
    public class MqttApplicationMessageInterceptor : IMqttServerApplicationMessageInterceptor
    {
        private readonly ILogger<MqttApplicationMessageInterceptor> _logger;
        private readonly Data_Storage _dataStorage;
        private readonly IConfiguration _configuration;

        public MqttApplicationMessageInterceptor(
            ILogger<MqttApplicationMessageInterceptor> logger,
            IConfiguration configuration
            )
        {
            _logger = logger;
            _configuration = configuration;
        }
        public Task InterceptApplicationMessagePublishAsync(MqttApplicationMessageInterceptorContext context)
        {
            // we do not process messages from ourselves
            if (string.IsNullOrEmpty(context.ClientId))
            {
                return Task.CompletedTask;
            }
            try
            {
                string payload = (context.ApplicationMessage.Payload.Length > 0) ? Encoding.UTF8.GetString(context.ApplicationMessage.Payload) : "";

                if (!string.IsNullOrEmpty(payload))
                {
                   
                }
                return Task.CompletedTask;
            }
            catch
            {
                return Task.CompletedTask;
            }
        }
    }
}
